package team.seekers.oj.codetest.controller;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import team.seekers.oj.codetest.dto.CodeTestCreateReqDTO;
import team.seekers.oj.codetest.dto.CodeTestListDTO;
import team.seekers.oj.codetest.dto.CodeTestListReqDTO;
import team.seekers.oj.codetest.dto.CodeTestManageDTO;
import team.seekers.oj.codetest.entity.vo.CodeTestInfoVO;
import team.seekers.oj.codetest.service.CodeTestService;
import team.seekers.oj.common.annotation.UserSession;
import team.seekers.oj.common.entity.ApiResponseBody;
import team.seekers.oj.common.entity.PageResult;
import team.seekers.oj.common.entity.UserSessionDTO;
import team.seekers.oj.common.enums.ApiExceptionEnum;
import team.seekers.oj.common.util.AssertUtils;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@Controller
@RequestMapping("/codetest")
public class CodeTestController {

    @Autowired
    private CodeTestService codeTestService;

    /**
     * 查询所有公开的题单列表
     * @return
     */
    @GetMapping("/page")
    @ApiResponseBody
    public PageResult<CodeTestListDTO> page(@Valid CodeTestListReqDTO reqDTO,
                                            @UserSession(nullable = true) UserSessionDTO userSessionDTO) {
        return codeTestService.page(reqDTO);
    }

    /**
     * 根据条件模糊查询所有的题单
     * @param reqDTO
     * @return
     */
    @PostMapping("/page/condition")
    @ApiResponseBody
    public PageResult<CodeTestListDTO> pageCondition(@RequestBody @Valid CodeTestListReqDTO reqDTO) {

        return codeTestService.pageCondition(reqDTO);
    }


    /**
     * 根据题单id查询对应题单的信息
     */
    @GetMapping("/info")
    @ApiResponseBody
    public CodeTestInfoVO query(@RequestParam("id") long id) {
        return codeTestService.getVO(id);
    }


    /**
     * 根据题单id查询对应的所有题目列表
     */
    @GetMapping("/problems")
    @ApiResponseBody
    public List<Long> queryIdToProblemIds(Long codeTestId) {
        return codeTestService.queryIdToProblemIds(codeTestId);
    }

    /**
     * 老师创建一个题单
     */
    @PostMapping("/create")
    @ApiResponseBody
    public Long create(@RequestBody @Valid CodeTestCreateReqDTO reqDTO,
                       @UserSession UserSessionDTO userSessionDTO) {
        // 增补
        reqDTO.setUserId(userSessionDTO.getUserId());

        List<Long> problemIds = Optional.ofNullable(reqDTO.getProblems()).orElse(Lists.newArrayList());
        System.out.println(problemIds);
        reqDTO.setProblems(problemIds.stream().distinct().collect(Collectors.toList()));

        return codeTestService.create(reqDTO);
    }

    /**
     * 老师删除一个题单
     */
    @GetMapping("/delete")
    @ApiResponseBody
    public Void delete(@RequestParam("codeTestId") long codeTestId,
                           @UserSession UserSessionDTO userSessionDTO) {
        // 创建者才可修改群组
        AssertUtils.isTrue(codeTestService.isCodeTestManager(codeTestService.getDTO(codeTestId).getUserId(), userSessionDTO),
                ApiExceptionEnum.USER_NOT_MATCHING, "没有权限");
        codeTestService.removeCodeTest(codeTestId);
        return null;
    }

    /**
     *老师修改个题单
     */
    @PostMapping("/update")
    @ApiResponseBody
    public Void update(@RequestBody @Valid CodeTestManageDTO reqDTO,
                       @UserSession UserSessionDTO userSessionDTO) {
        // 创建者才可修改群组
        AssertUtils.isTrue(codeTestService.isCodeTestManager(reqDTO.getUserId(), userSessionDTO),
                ApiExceptionEnum.USER_NOT_MATCHING, "没有权限");

        List<Long> problems = Optional.ofNullable(reqDTO.getProblems()).orElse(Lists.newArrayList());
        reqDTO.setProblems(problems.stream().distinct().collect(Collectors.toList()));

        codeTestService.update(reqDTO);
        return null;
    }

    /**
     * 题单中新增一个题目
     */
    @GetMapping("/problemIn")
    @ApiResponseBody
    public Void problemIn(@RequestParam("codeTestId") long codeTestId,
                          @RequestParam("problemId") long problemId,
                          @UserSession UserSessionDTO userSessionDTO) {
        // 创建者才可修改群组
        AssertUtils.isTrue(codeTestService.isCodeTestManager(codeTestService.getDTO(codeTestId).getUserId(), userSessionDTO),
                ApiExceptionEnum.USER_NOT_MATCHING, "没有权限");
        codeTestService.addProblem(codeTestId,problemId);
        return null;
    }

    /**
     *题单中删除一个题目
     */
    @GetMapping("/problemOut")
    @ApiResponseBody
    public Void problemOut(@RequestParam("codeTestId") long codeTestId,
                           @RequestParam("problemId") long problemId,
                           @UserSession UserSessionDTO userSessionDTO) {
        // 创建者才可修改群组
        AssertUtils.isTrue(codeTestService.isCodeTestManager(codeTestService.getDTO(codeTestId).getUserId(), userSessionDTO),
                ApiExceptionEnum.USER_NOT_MATCHING, "没有权限");
        codeTestService.removeProblem(codeTestId,problemId);
        return null;
    }

    /**
     * 学生收藏（加入）一个题单
     */
    @GetMapping("/studentIn")
    @ApiResponseBody
    public Void join(@RequestParam("codeTestId") long codeTestId,
                     @UserSession UserSessionDTO userSessionDTO) {
        codeTestService.addUser(codeTestId,userSessionDTO.getUserId());
        return null;
    }


    /**
     * 学生退出一个题单
     */
    @GetMapping("/studentOut")
    @ApiResponseBody
    public Void out(@RequestParam("codeTestId") long codeTestId,
                    @UserSession UserSessionDTO userSessionDTO) {
        codeTestService.removeUser(codeTestId,userSessionDTO.getUserId());
        return null;
    }





}
